﻿using System.Collections.Specialized;
using System.IO;
using Rodunu.Movies.Contracts;
using Rodunu.Movies.Services.Interfaces.Netflix;
using Rodunu.Movies.Services.Models.Netflix;
using Rodunu.Movies.Services.NetFlix.DataProvider;

namespace Rodunu.Movies.Services.NetFlix.Logic
{
    public class Search : NetflixServiceBase, ISearch
    {
        #region Constructors

        public Search(string tokenKey)
            : base(tokenKey)
        {
            
        }

        public Search()
        {

        }

        public Search(UserInfo userId)
            : base(userId)
        {

        }

        #endregion

        #region ISearch Members

        public CatalogList SearchCatalog(string term, int startIndex, int maxResults)
        {
            var parameters = new NameValueCollection();
            parameters.Add("term", term);
            parameters.Add("start_index", startIndex.ToString());
            parameters.Add("max_results", maxResults.ToString());
            return (CatalogList)
                   _serializationHelper.FromXml(typeof (CatalogList),
                                                new StreamReader(
                                                    _netflixService.GetUnprotected(
                                                        "http://api.netflix.com/catalog/titles",
                                                        parameters).GetResponseStream()).
                                                    ReadToEnd());
        }

        #endregion
    }
}
